package com.xqyq.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

@Controller
public class FileUploadController {

	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	public static String handleUploadData(String name,
			@RequestParam("file") CommonsMultipartFile file,
			HttpServletRequest req) {
		if (!file.isEmpty()) {
			String path = Thread.currentThread().getContextClassLoader()
					.getResource("").getPath()
					+ "download";
			System.out.println(path);
			String fileName = file.getOriginalFilename();
			String fileType = fileName.substring(fileName.lastIndexOf("."));
			System.out.println(fileType);
			String fileurl = new Date().getTime() + fileType;

			File file2 = new File(path, fileurl);
			try {
				file.getFileItem().write(file2);
			} catch (Exception e) {
				e.printStackTrace();
			}

			/*
			 * User user = (User) req.getSession().getAttribute("OnlineUser");
			 * if (user == null) return null;
			 * 
			 * System.out.println(user.getUserId());
			 * user.setUserlogo("http://localhost:8080/Announcement/upload/" +
			 * userlogo); System.out.println(user.getUserlogo()); boolean status
			 * = userService.updateUser(user); if (status) return
			 * "redirect:/message/showArticleToHome?pageIndex=1"; return
			 * "uploadface"; } else { return "uploadface"; }
			 */
			return fileurl;
		}
		return null;
	}



	@RequestMapping("/download")
	public String download(String fileName, HttpServletRequest request,
			HttpServletResponse response) {
		response.setCharacterEncoding("utf-8");
		response.setContentType("multipart/form-data");
		response.setHeader("Content-Disposition", "attachment;fileName="
				+ fileName);
		try {
			String path = Thread.currentThread().getContextClassLoader()
					.getResource("").getPath()
					+ "download";// 这个download目录为啥建立在classes下的
			System.out.println(fileName);
			InputStream inputStream = new FileInputStream(new File(path,
					fileName));

			OutputStream os = response.getOutputStream();
			byte[] b = new byte[2048];
			int length;
			while ((length = inputStream.read(b)) > 0) {
				os.write(b, 0, length);
			}

			// 这里主要关闭。
			os.close();

			inputStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 返回值要注意，要不然就出现下面这句错误！
		// java+getOutputStream() has already been called for this response
		return null;
	}
	/*
	 * @RequestMapping("download") public void download(HttpServletResponse res)
	 * throws IOException { System.out.println("download");
	 * 
	 * OutputStream os = res.getOutputStream(); try { res.reset();
	 * res.setHeader("Content-Disposition", "attachment; filename=dict.txt");
	 * res.setContentType("application/octet-stream; charset=utf-8");
	 * os.write(FileUtils.readFileToByteArray(new File("E:\\ME\\001.xls")));
	 * os.flush(); } finally { if (os != null) { os.close(); } } }
	 */

}
